import { Context } from 'koa';
import { DicService } from '../../services/system/dicService';
import { createError } from '../../middleware/errorHandler';
import { CreateDicDto, UpdateDicDto } from '../../dtos/system/DicDto';
import { generateResponse } from '../../utils';

export class DicController {
    private dicService: DicService;

    constructor() {
        this.dicService = new DicService();
    }

    // 获取所有字典项
    getDics = async (ctx: Context): Promise<void> => {
        try {
            let data = ctx.request.body as any
            let dics = [] as any
            if (data?.page && data?.limit) {

                dics = await this.dicService.getDicsWithPagination(data.page, data.limit);
                console.log(dics, 'dics');
            } else {
                dics = await this.dicService.getAllDics();
            }
            generateResponse(ctx, dics);
        } catch (error: any) {
            throw createError('获取字典项列表失败', 500, error);
        }
    };

    // 根据Type获取字典项
    getDicByType = async (ctx: Context): Promise<void> => {
        try {
            const { type } = ctx.params;
            const dic = await this.dicService.getDicByType(type);

            if (!dic) {
                throw createError('字典项不存在', 404);
            }

            generateResponse(ctx, dic);
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('获取字典项信息失败', 500, error);
        }
    };

    // 创建用户
    createDic = async (ctx: Context): Promise<void> => {
        try {
            // 从验证中间件获取已验证的数据
            const dicData = ctx.state.validatedData as CreateDicDto;
            console.log(dicData, 'dicDa222222222222ta')
            const dic = await this.dicService.createDic(dicData);
            generateResponse(ctx, dic, '字典项创建成功');
        } catch (error: any) {

            throw createError('创建字典项失败1', 400, error);
        }
    };

    // 更新用户
    updateDic = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            // 从验证中间件获取已验证的数据
            const dicData = ctx.state.validatedData as UpdateDicDto;

            const dic = await this.dicService.updateDic(id, dicData);

            if (!dic) {
                throw createError('字典项不存在', 404);
            }


            generateResponse(ctx, dic, '字典项更新成功');
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('更新字典项失败', 400, error);
        }
    };

    // 删除用户
    deleteDic = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            const result = await this.dicService.deleteDic(id);

            if (!result) {
                throw createError('字典项不存在', 404);
            }


            generateResponse(ctx, null, '字典项删除成功');
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('删除字典项失败', 500, error);
        }
    };
}